<?php class myFunctions extends saja {
######################################

var $thisfile;
var $scheme;

function __construct() {
    $this->scheme = 'local';
    $docRoot = getenv('DOCUMENT_ROOT');
    $this->thisfile =  $docRoot . 'common/file_systems/local-fs_saja-lib.php';   
}
function browse_local($directory) {
    $_SESSION['local_path'] = $directory;
    if (strpos(strtolower($directory), "$this->scheme://") === 0) {
        $directory = substr($directory, strlen("$this->scheme://") - 1);
        if (rtrim($directory,'/') != '') {
            $directory = rtrim($directory,'/') . '/';
        }
        if (is_dir($directory)) {
            $this->build_quick_link($this->scheme, $directory);
            $results = array();
            $handler = opendir($directory);
            while ($fname = readdir($handler)) {
                if ($fname != '.' && $fname != '..') {
                    if ($fname != '') {
                        $file['name'] = $fname;
                        if (is_dir($directory . $fname)) {
                            $file['type'] = 'dir';
                            $file['size'] = 0;
                        } else {
                            $file['type'] = 'file';
                            $file['size'] = filesize($directory . $fname);
                        }
                        #$file['modified'] = $flastmod;
                        $foldercontents[] = $file;
                    }
                }
            }
            closedir($handler);
            # Display Results
            if (count($foldercontents) > 0) {
                $thisimg = '/common/images/Folder-48x48.png';
                foreach ($foldercontents as $file) {
                    if ($file['type'] == 'dir') {
                        $fname = $file['name'];
                        $newpath = "$this->scheme:/" . $directory . $fname;
                        $inputid = $this->add_hidden_input($newpath);
                        $applink = $this->run("browse_local(".$inputid.":value)->$this->target_id",$this->thisfile);
                        $res['title'] = $fname;
                        $res['link'] = $applink;
                        $res['img'] = $thisimg;
                        $results[] = $res; 
                    }
                }
                $this->display_results($results);
                $results = null;
                $thisimg = '/common/images/mac-48x48.png';
                foreach ($foldercontents as $file) {
                    if ($file['type'] == 'file') {
                        $fname = $file['name'];
                        $newpath = "$this->scheme:/" . $directory . $fname;
                        $inputid = $this->add_hidden_input($newpath);
                        $applink = $this->run("show_file_prop(".$inputid.":value)",$this->thisfile);
                        ###### FINISH Code for file access
                        $res['title'] = $fname;
                        $res['link'] = $applink;
                        $res['img'] = $thisimg;
                        $res['href'] = ''; #'#ifile_quickview';
                        $res['details'] = $this->conv_bytes($file['size']);
                        $results[] = $res;             
                    }
                }
                $this->display_results($results);
                $results = null; 
            }
        }
    }
}
function file_get($host, $user, $pass, $share, $srcfile, $destfile) {
    $srcfile = $this->clean($srcfile, '\\',false);
    $destfile = $this->clean($destfile, '/',false);    
    $fullcmd = 'smbclient //' . $host . '/' . $share;
    if ($user != '') {
        $fullcmd .= ' -U ' . $user;
        if ($pass != '') {
            $fullcmd .= '%' . $pass;
        }        
    }
    $fullcmd .= " -c 'get \"$srcfile\" \"$destfile\";'";
    $ret = $fullcmd . " at " . time() . "<br />\n";
    $shell_ret = '';
    $shell_ret = shell_exec($fullcmd);
    if ($shell_ret == '') {
        $ret .= "File transfered.<br />\n";
    } else {
        $ret .= "$shell_ret<br />\nError<br />\n";
    }
    return $ret;
}
function file_put($host, $user, $pass, $share, $srcfile, $destfile) {
    
    $srcfile = $this->clean($srcfile, '\\',false);
    $destfile = $this->clean($destfile, '/',false);    
    $fullcmd = 'smbclient //' . $host . '/' . $share;
    if ($user != '') {
        $fullcmd .= ' -U ' . $user;
        if ($pass != '') {
            $fullcmd .= '%' . $pass;
        }        
    }
    $fullcmd .= " -c 'put \"$srcfile\" \"$destfile\";'";
    $ret = $fullcmd . " at " . time() . "<br />\n";
    $shell_ret = '';
    $shell_ret = shell_exec($fullcmd);
    if ($shell_ret == '') {
        $ret .= "File transfered.<br />\n";
    } else {
        $ret .= "$shell_ret<br />\nError<br />\n";
    }
    return $ret;
}
function clean($path, $use_slash, $remove_preceding_slash) {
    $path = trim($path);
    if ($use_slash == '') {
        $use_slash = '/';   
    }
    if (empty($path)) {
        $path = JPATH_ROOT;
    } else {
        // Remove double slashes and backslahses and convert all slashes and backslashes to DS
        $outpath = ereg_replace("\.[\.]+", "", $path);
        if ($remove_preceding_slash) {
            $outpath = ereg_replace("^[\/]+", "", $outpath);
        }
        $outpath = preg_replace('#[/\\\\]+#', $use_slash, $outpath);
        $outpath = ereg_replace("^[A-Za-z][:\|][\/]?", "", $outpath);
    }
    return $outpath;
}
function clear() {
    return '';   
}

######################################
} ?>